package resources.levels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import me.ak.sf.Sloth;
import me.ak.sf.SlothfallGame;
import me.ak.sf.SlothfallGameState;
import static me.ak.sf.SlothfallGameState.sloths;
import me.ak.sf.level.Level;
import me.ak.sf.util.LevelTimer;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

public class Level1 extends Level implements ActionListener {

        public Level1() {

        }

        @Override
        public void init(SlothfallGameState parent) {
                super.init(parent);
                time.start();
        }

        @Override
        public int getId() {
                return 0;
        }

        @Override
        public void renderBackground(Graphics g) {

        }

        @Override
        public void renderForeground(Graphics g) {
                g.setColor(Color.white);
                long t = time.total-time.getTimePassed();
                g.drawString(String.format("Level Time: %d:%d",
                        TimeUnit.MILLISECONDS.toMinutes(t),
                        TimeUnit.MILLISECONDS.toSeconds(t)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(t))
                ), 0, 0);
        }

        public BufferedImage capture(Graphics g) {
                ByteBuffer bb = ByteBuffer.allocate(SlothfallGame.WIDTH * SlothfallGame.HEIGHT);
                g.getArea(0, 0, SlothfallGame.WIDTH, SlothfallGame.HEIGHT, bb);

                BufferedImage img = new BufferedImage(SlothfallGame.WIDTH, SlothfallGame.HEIGHT, BufferedImage.TYPE_INT_RGB);
                WritableRaster wr = img.getRaster();
                wr.setPixels(0, 0, SlothfallGame.WIDTH, SlothfallGame.HEIGHT, bb.asIntBuffer().array());
                return img;
        }

        public long[] slothWaitLimits = new long[]{1500, 2000};
        public long slothWaitingTime = 2000;
        public Random rand = new Random();

        public long timeOfLastSloth = System.currentTimeMillis();

        public LevelTimer time = new LevelTimer(1000 * 60, this);
        public boolean running = true;

        @Override
        public void update() {
                boolean spawnNewSloth = SlothfallGame.rand.nextInt(1500) < 5 && System.currentTimeMillis() - timeOfLastSloth > slothWaitingTime;
                if (spawnNewSloth) {
                        sloths.add(new Sloth(parent.thePlayer.x, -300, 0.75f));
                        slothWaitingTime = nextLong(rand, slothWaitLimits[0] - slothWaitLimits[1]) + slothWaitLimits[0];
                        timeOfLastSloth = System.currentTimeMillis();
                }
                for (Sloth s : sloths) {
                        s.update();
                        if (s.getBounds().intersects(parent.thePlayer.getBounds())) {
                                sloths.clear();
                                parent.parent.enterState(2);
                        }
                }
                parent.thePlayer.x += parent.playerMovement;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
                running = false;
        }

        long nextLong(Random rng, long n) {
                // error checking and 2^x checking removed for simplicity.
                long bits, val;
                do {
                        bits = (rng.nextLong() << 1) >>> 1;
                        val = bits % n;
                } while (bits - val + (n - 1) < 0L);
                return val;
        }

}
